/* fafamte.p - FIXED ASSETS MAINTENANCE - UOP Audit                     */
/* Copyright 1986 QAD Inc. All rights reserved.                               */
/* $Id:: fafamte.p 59128 2015-08-21 10:49:10Z nsc                          $: */
/* $Revision: 1.10.1.10 $                                                         */
/*V8:ConvertMode=NoConvert                                              */
/* REVISION: 9.1     LAST MODIFIED: 10/26/99     BY: *N021* Pat Pigatti */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00 BY: *N08T* Annasaheb Rahane */
/* REVISION: 9.1      LAST MODIFIED: 07/28/00   BY: *N0BX* Arul Victoria    */
/* REVISION: 9.1      LAST MODIFIED: 08/14/00   BY: *N0L0* Jacolyn Neder    */
/* Revision: 1.10.1.9  BY: Paul Donnelly (SB) DATE: 06/26/03 ECO: *Q00C* */
/* $Revision: 1.10.1.10 $    BY: Vandna Rohira         DATE: 12/17/04  ECO: *P2Z5*  */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/
/*-Revision end---------------------------------------------------------------*/

{us/bbi/mfdeclre.i}
{us/bbi/gplabel.i} /* EXTERNAL LABEL INCLUDE */

/* -------------  AUDIT TEMP TABLE DEFINITION  ------------- */
define temp-table t_audit
   field t_yrper       like fauop_yrper
   field t_fauop_upper like fauop_upper
   field t_fauop_accup like fauop_accup
   field t_fabd_upper  like fabd_upper
   field t_fabd_accup  like fabd_accup
   index t_yrper is unique primary t_yrper.

/* --------------------  Define Query  ------------------- */
define query q_t_audit for
   t_audit
   scrolling.

/* -----------------  Standard Variables  ---------------- */
define variable p-status as character no-undo.
define variable perform-status as character format "x(25)"
   initial "first" no-undo.
define variable l-rowid as rowid no-undo.
define variable l-top-rowid as rowid no-undo.
define variable lines as integer initial 10 no-undo.
define variable pos as integer no-undo.
define variable l-first like mfc_logical no-undo.

/* ------------------  Button Variables  ----------------- */
define button b-end label "End".

/* -------------  Standard Widget Variables  ------------- */
define variable l-focus as widget-handle no-undo.
define variable w-frame as widget-handle no-undo.

/* ------------------  Screen Variables  ----------------- */
define shared variable l-fa-id as character format "x(12)" no-undo.
define shared variable l-fabk-id as character format "x(12)" no-undo.

/* ------------------  Frame Definition  ----------------- */
/* Added side-labels phrase to frame statement              */

define frame f-button
   b-end at 1
with no-box overlay three-d side-labels width 10.

/* CLEAR FRAME REGISTRATION TO TRANSLATE THE FRAME EVERYTIME*/
clearFrameRegistration(frame f-button:handle).

/* SET EXTERNAL LABELS */
setFrameLabels(frame f-button:handle).

assign
   l-focus = b-end:handle in frame f-button
   b-end:width = 8
   b-end:private-data = "end".

define frame f-1
   skip(.4)
   space(13)
   t_yrper colon 8
   t_fauop_upper
   t_fauop_accup
   t_fabd_upper
   t_fabd_accup
with three-d overlay 8 down scroll 1 width 80.

/* CLEAR FRAME REGISTRATION TO TRANSLATE THE FRAME EVERYTIME*/
clearFrameRegistration(frame f-1:handle).

/* SET EXTERNAL LABELS */
setFrameLabels(frame f-1:handle).

run ip-framesetup.

w-frame = frame f-1:handle.
{us/bbi/gprun.i ""fafmtut.p"" "(w-frame)"}

lines = 08.

run ip-create-temp-table(l-fa-id,
   l-fabk-id).

open query q_t_audit for each t_audit no-lock.

get first q_t_audit no-lock.

main-loop:
do while perform-status <> "end" on error undo:

   run ip-query
      (input-output perform-status,
      input-output l-rowid).

   /* ----------------------  Display  ---------------------- */
   run ip-predisplay.

   /* ----------------  Selection Buttons  ---------------- */
   if perform-status <> "first"
   then
      run ip-button
         (input-output perform-status).

   /* -----------------------  End  ----------------------- */
   if perform-status = "end"
   then do:
      hide frame f-1 no-pause.
      delete PROCEDURE this-procedure no-error.
      leave.
   end.  /* IF perform-status = "end"  */
end.  /* MAIN-LOOP */

PROCEDURE ip-predisplay:
   if perform-status = "" and
      available t_audit
   then
      display-loop:
      do:
         clear frame f-1 all no-pause.
         l-top-rowid = rowid(t_audit).
         read-loop:
         do pos = 1 to lines:
            if session:display-type = "gui"
            then
               assign
                  t_yrper:bgcolor = 8
                  t_yrper:fgcolor = 0.
            else
               color display normal t_yrper
               with frame f-1.
            run ip-display.
            if pos < lines
            then
               down with frame f-1.
            get next q_t_audit no-lock.
            if not available t_audit
            then
               leave.
         end.  /* READ-LOOP */
         run ip-postdisplay.
      end.  /* DISPLAY-LOOP */
END PROCEDURE. /* ip-predisplay */

PROCEDURE ip-display:
   if available t_audit
   then
      display
         t_audit.t_yrper
         t_audit.t_fauop_upper
         t_audit.t_fauop_accup
         t_audit.t_fabd_upper
         t_audit.t_fabd_accup
      with frame f-1.
END PROCEDURE. /* ip-display */

PROCEDURE ip-postdisplay:
   do pos = 1 to lines:
      if frame-line(f-1) <= 1
      then
         leave.
      up 1 with frame f-1.
   end.  /* DO pos = 1 TO LINES */
   if perform-status = ""
   then do:
      reposition q_t_audit to rowid l-top-rowid no-error.
      get next q_t_audit no-lock.
      l-rowid = l-top-rowid.
      color display message t_yrper
      with frame f-1.
   end.  /* IF perform-status = ""  */
END PROCEDURE. /* ip-postdisplay */

PROCEDURE ip-query:
   define input-output parameter perform-status as character
      no-undo.
   define input-output parameter l-rowid as rowid no-undo.

   if perform-status = "first"
   then do:
      if l-rowid <> ?
      then do:
         reposition q_t_audit to rowid l-rowid no-error.
         get next q_t_audit no-lock.
      end.  /* IF l-rowid <> ?  */
      if not available t_audit
      then
         get first q_t_audit no-lock.
      if available t_audit
      then
         assign
            perform-status = ""
            l-rowid = rowid(t_audit).
      else do:
         assign
            perform-status = ""
            l-rowid = ?
            frame f-1:visible = true.
         return.
      end.  /* IF NOT AVAILABLE t_audit  */
   end.  /* IF perform-status = "first"  */
   if perform-status = "open"
   then do:
      open query q_t_audit for each t_audit no-lock.

      reposition q_t_audit to rowid l-rowid no-error.
      get next q_t_audit no-lock.
      if available t_audit
      then
         assign
            perform-status = ""
            l-rowid = rowid(t_audit).
      else do:
         get first q_t_audit no-lock.
         if not available t_audit
         then do:
            assign
               perform-status = ""
               frame f-1:visible = true.
            return.
         end.  /* IF NOT AVAILABLE t_audit  */
         else
         assign
            perform-status = ""
            l-rowid = rowid(t_audit).
      end.  /* IF NOT AVAILABLE t_audit  */
   end.  /* IF perform-status = "open"  */
   if perform-status = "next"
   then do:
      get next q_t_audit no-lock.
      if available t_audit
      then do:
         hide message no-pause.
         assign
            l-rowid = rowid(t_audit)
            perform-status = "next".
         if session:display-type = "gui"
         then
            assign
               t_yrper:bgcolor = 8
               t_yrper:fgcolor = 0.
         else
            color display normal t_yrper
            with frame f-1.
         pause 0. /* MAKES SCROLLING WORK - DON'T REMOVE */
         down 1 with frame f-1.
         run ip-display.
         color display message t_yrper
         with frame f-1.
      end.  /* IF AVAILABLE t_audit  */
      else do:
         perform-status = "next".
         /* END OF FILE */
         {us/bbi/pxmsg.i &MSGNUM=20 &ERRORLEVEL=2}
         get last q_t_audit no-lock.
      end.  /* IF AVAILABLE t_audit  */
   end.  /* IF perform-status = "next"  */
   if perform-status = "prev"
   then do:
      get prev q_t_audit no-lock.
      if not available t_audit
      then do:
         {us/bbi/pxmsg.i &MSGNUM=21 &ERRORLEVEL=2} /* BEGINNING OF FILE */
         get first q_t_audit no-lock.
         return.
      end.  /* IF NOT AVAILABLE t_audit  */
      hide message no-pause.
      l-rowid = rowid(t_audit).
      if session:display-type = "gui"
      then
         assign
            t_yrper:bgcolor = 8
            t_yrper:fgcolor = 0.
      else
         color display normal t_yrper
         with frame f-1.
      pause 0.  /* MAKES SCROLLING WORK - DON'T REMOVE */
      up 1 with frame f-1.
      run ip-display.
      color display message t_yrper
      with frame f-1.
   end.  /* IF perform-status = "prev"  */
END PROCEDURE. /* ip-query */

PROCEDURE ip-framesetup:
   if session:display-type = "gui"
   then do:
      assign
         frame f-1:row = 4.5
         frame f-1:overlay = true.
   end.  /* IF session:display-type = "gui"  */
   else
      frame f-1:row = 4.

   assign
      frame f-1:box = true
      frame f-1:centered = true
      frame f-1:title = (getFrameTitle("UOP_AUDIT",15))
      frame f-button:centered = true
      frame f-button:row = 20.
END PROCEDURE. /* ip-framesetup */

PROCEDURE ip-button:
   define input-output parameter perform-status as character
      format "x(25)" no-undo.
   enable all with frame f-button.
   ststatus = stline[1].
   status input ststatus.

   on choose of b-end
   do:
      assign
         perform-status = self:private-data
         l-focus = self:handle.
      hide frame f-1 no-pause.
      hide frame f-button no-pause.
   end.  /* ON CHOOSE OF b-end  */

   on cursor-up, f9 anywhere
   do:
      assign
         perform-status = "prev"
         l-focus = focus:handle.
      apply "go" to frame f-button.
   end.  /* ON CURSOR-UP, F9 ANYWHERE  */

   on cursor-down, f10 anywhere
   do:
      assign
         perform-status = "next"
         l-focus = focus:handle.
      apply "go" to frame f-button.
   end.  /* ON CURSOR-DOWN, F10 ANYWHERE  */

   on cursor-right anywhere
   do:
      l-focus = self:handle.
      apply "tab" to l-focus.
   end.  /* ON CURSOR-RIGHT ANYWHERE  */

   on cursor-left anywhere
   do:
      l-focus = self:handle.
      if session:display-type = "gui"
      then
         apply "shift-tab" to l-focus.
      else
         apply "ctrl-u" to l-focus.
   end.  /* ON CURSOR-LEFT ANYWHERE  */

   on esc anywhere
   do:
      if session:display-type = "gui"
      then
         apply "choose" to b-end in frame f-button.
   end.  /* ON ESC ANYWHERE  */

   on end-error anywhere
      apply "choose" to b-end in frame f-button.

   on window-close of current-window
      apply "choose" to b-end in frame f-button.

   on go anywhere
   do:
      if (lastkey = keycode("cursor-down") or
         lastkey = keycode("cursor-up") or
         lastkey = keycode("f9") or
         lastkey = keycode("f10"))
      then
         return.
      else
         l-focus = focus:handle.
      apply "choose" to l-focus.
   end.  /* ON GO ANYWHERE  */

   wait-for
      go of frame f-button or
      window-close of current-window or
      choose of
      b-end
      focus
      l-focus.
   hide message no-pause.
END PROCEDURE. /* ip-button */

PROCEDURE ip-create-temp-table:

   define input parameter l-fa-id like fabd_fa_id.
   define input parameter l-fabk-id like fabd_fa_id.

   define variable l-final-yrper like fauop_yrper no-undo.

   /* FIND THE LAST PERIOD FOR FAUOP_DET */
   for last fauop_det
      where fauop_det.fauop_domain =
      global_domain and
      fauop_fa_id = l-fa-id and
      fauop_fabk_id = l-fabk-id
   no-lock:
   l-final-yrper = fauop_yrper.
   end.  /* FOR LAST fauop_det */

   /* LOOP THROUGH fauop_det, RETRIEVE ACTUAL DATA FROM   */
   /* CORRESPONDING fabd_det. BOTH fauop_det and fabd_det */
   /* STARTS FROM THE ACQUISITION PERIOD                  */
   for each fauop_det
         where fauop_det.fauop_domain =
         global_domain and
         fauop_fa_id = l-fa-id and
         fauop_fabk_id = l-fabk-id
    no-lock:

      /* RETRIEVE CORRESPONDING ACTUAL RECORDS */
      for first fabd_det
         where fabd_det.fabd_domain =
         global_domain and
         fabd_fa_id   = l-fa-id and
         fabd_fabk_id = l-fabk-id and
         fabd_yrper   = fauop_yrper
      no-lock:
      end.  /* FOR FIRST fabd_det */

      create t_audit.

      assign
         t_yrper       = fauop_yrper
         t_fauop_upper = fauop_upper
         t_fauop_accup = fauop_accup
         t_fabd_upper  = if available fabd_det
                         then
                            fabd_upper
                         else
                            0
         t_fabd_accup  = if available fabd_det
                         then
                            fabd_accup
                         else
                            0.
   end.  /* FOR EACH fauop_det */

   for each fabd_det
         where fabd_det.fabd_domain = global_domain
         and
         fabd_fa_id = l-fa-id and
         fabd_fabk_id = l-fabk-id and
         fabd_yrper > l-final-yrper
   no-lock:
      create t_audit.
      assign
         t_yrper = fabd_yrper
         t_fauop_upper = 0
         t_fauop_accup = 0
         t_fabd_upper = fabd_upper
         t_fabd_accup = fabd_accup.
   end.  /* FOR EACH fabd_det */
END PROCEDURE. /* ip-create-temp-table */
